﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using rock.DAL;
using rock.UI.common;
using rock.BLL;
using rock.common;
 

namespace rock.UI.houseinfo
{
    public partial class UserControlRentContract : UserControl
    {
        private int CURRENT_PAGE = 1;
        DalRENT_CONTRACT dal = new DalRENT_CONTRACT();
        BllRENT_CONTRACT bllRENT_CONTRACT = new BllRENT_CONTRACT();

        DalHOUSE_THING dalHOUSE_THING = new DalHOUSE_THING();
        public UserControlRentContract()
        {
            InitializeComponent();
 
                       

          //  this.userControlCommonListHeader1.selectClick = selectClick;

            this.Dock = System.Windows.Forms.DockStyle.Fill;

            this.userControlCommonListPage1.pageClick = refreshData;

            this.userControlLeftTree1.selectedTreeinfo = selectedTreeinfo;

            
            ViewUtils.setDataGridViewStlye(this.dataGridView2);
            ViewUtils.setDataGridViewStlye(this.dataGridView1);
        }

        private void UserControlRentCustom_Load(object sender, EventArgs e)
        {
            refreshData(1);

        }
        int XiaoQuSeqid = 0;
        int DongSeqid = 0;
        int ContractStatus = -1;
        private void refreshData(int goPage)
        {
            CURRENT_PAGE = goPage;
            String search = this.userControlCommonListHeader1.textBox1.Text;
            StringBuilder where = new StringBuilder(
                 "(" +
                        " HOUSE_ROOM.HOUSE_NUMBER LIKE '%" + search + "%' " +
               " OR " + " TEMP.DONG_NAME LIKE '%" + search + "%' " +
               " OR " + " TEMP.XIAOQU_NAME LIKE '%" + search + "%' " +
               " OR " + " RENT_CUSTOM.NAME LIKE '%" + search + "%' " +
               ")"
              );
            if (XiaoQuSeqid > 0)
            {
                where.Append(" AND TEMP.XIAOQU_SEQID =  " + XiaoQuSeqid);
            }
            if (DongSeqid > 0)
            {
                where.Append(" AND TEMP.DONG_SEQID =  " + DongSeqid);
            }
            if (ContractStatus != -1) {
                where.Append(" AND RENT_CONTRACT.STATUS =  " + ContractStatus);
            }

            List<int> status = new List<int>();
            if (checkBoxShengxiao.Checked)
            {
                status.Add(Constant.RENT_CONTRACT_STATUS_SHENGXIAO);
            }

            if (this.checkBoxDongzhi.Checked)
            {
                status.Add(Constant.RENT_CONTRACT_STATUS_DONGZHI);
            }

            if (this.checkBoxFuiChu.Checked)
            {
                status.Add(Constant.RENT_CONTRACT_STATUS_FEICHU);
            }

            if (status.Count > 0)
            {
                where.Append(" AND RENT_CONTRACT.STATUS in (  " + StringUtils.list2sqlinIds(status) + ")");
            }


            PageDataSet ds = dal.GetListByPage(where.ToString(), "  STATUS ASC ,  STOP_DATE DESC ", CURRENT_PAGE, Manager.getInstance().pageSize);
            this.dataGridView1.DataSource = ds.DataSet.Tables[0];
            this.userControlCommonListPage1.Inits(ds.AllRecord, this.CURRENT_PAGE);
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            buttonUpdate_Click(null, null);
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            FormRentContractUpdate fcv = new FormRentContractUpdate();
            fcv.oneRefreshParentData = refreshData;
            fcv.ShowDialog(this);
        }

        private void buttonUpdate_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {



                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                // this.textBox1.Text = value;
                FormRentContractUpdate fcv = new FormRentContractUpdate();



                fcv._ModelRENT_CONTRACT = dal.GetModel(Convert.ToInt32(seqid));
                //注册代理
                fcv.oneRefreshParentData = refreshData;
                fcv.ShowDialog(this);

            }
            else
            {
                MessageBox.Show("请选择要更新记录.", "提示");
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                String status = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["STATUS_INT"].Value.ToString();
                if (Convert.ToInt32(status) == Constant.RENT_CONTRACT_STATUS_SHENGXIAO) {
                    MessageBox.Show("此合同正在生效,不能删除.", "提示");
                    return;
                }

                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {


                    DialogResult dialogResult = MessageBox.Show("请确认是否要删除", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        bllRENT_CONTRACT.Delete(seqidInt); 
                        //dal.Delete(seqidInt);
                        //刷新数据
                        refreshData(CURRENT_PAGE);
                    }


                }
            }
            else
            {
                MessageBox.Show("请选择要删除的记录.", "提示");
            }
        }

        private void buttonSelect_Click(object sender, EventArgs e)
        {
            refreshData(CURRENT_PAGE);
        }

        

         
        
       
        private void UserControlHouseRoom_Load(object sender, EventArgs e)
        {
            refreshData(1);
        }

        private void selectClick()
        {

             
        }

        private void selectedTreeinfo(TreeInfo treeinfo)
        {
            if (treeinfo.NodeType == 0)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 1)
            {
                this.XiaoQuSeqid = treeinfo.Id;
                this.DongSeqid = 0;
            }
            else if (treeinfo.NodeType == 2)
            {
                this.XiaoQuSeqid = 0;
                this.DongSeqid = treeinfo.Id;
            }
          
            this.refreshData(this.CURRENT_PAGE);
        }


         #region 更换状态图片
        private void dataGridView1_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            //loadImageWithDataGrid(this.dataGridView1 , "STATUS");
            setStatusWithDataGrid(dataGridView1);
        }
        public void setStatusWithDataGrid(DataGridView dgv)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["STATUS_INT"].Value;

                    DataGridViewTextBoxCell dgvcc = (DataGridViewTextBoxCell)dgv.Rows[a].Cells["STATUS_STR"];
                    dgvcc.Value = Constant.getByContractStatus(status).Name;
                }
                catch (Exception)
                {

                }
            }
        }

        public void loadImageWithDataGrid(DataGridView dgv, String colsName)
        {
            for (int a = 0; a < dgv.Rows.Count; a++)
            {

                try
                {

                    int status = (int)dgv.Rows[a].Cells["STATUS_INT"].Value;
                    String name = "";
                    switch (status)
                    {
                        case Constant.RENT_CONTRACT_STATUS_SHENGXIAO:
                            name = "green";
                            break;
                        case Constant.RENT_CONTRACT_STATUS_DONGZHI:
                            name = "red";
                            break;
                        case Constant.RENT_CONTRACT_STATUS_FEICHU:
                            name = "yellow";
                            break;
                         
                        default:
                            break;
                    }
                    DataGridViewImageCell dgvcc = (DataGridViewImageCell)dgv.Rows[a].Cells[colsName];

                    dgvcc.Value = Image.FromFile(Application.StartupPath + "\\image\\status\\" + name + ".png");
                    dgvcc.ToolTipText = Application.StartupPath + "\\image\\status\\" + name + ".png";

                }
                catch (Exception)
                {

                }

            }
            // DataGridViewTextBoxCell dgvcc2 = (DataGridViewTextBoxCell)dgv.Rows[1].Cells["NAME"];
            // dgvcc2.Value = "TTTTTTTTTTTTTTT";

        }
        #endregion 更换状态图片
        #region 房屋设备
        private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
        {
            refreshDataRoomThing();
        }

        //刷新数据
        public void refreshDataRoomThing()
        {

            if (this.dataGridView1.SelectedCells.Count == 0 || dataGridView1.CurrentRow==null)
            {
                return;
            }

            //MessageBox.Show(dataGridView1.CurrentRow.Index + "");

            String room_seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["HOUSE_ROOM_SEQID"].Value.ToString();
            if (room_seqid == null || room_seqid.Equals(""))
            {
               // MessageBox.Show("请先选择要管理的房间.");
                return;
            }
            DataSet ds = dalHOUSE_THING.GetList("ROOM_SEQID  =  " + Convert.ToInt32(room_seqid));
            this.dataGridView2.DataSource = ds.Tables[0];


        }
        #endregion 房屋设备

        

         

       
        //终止合同
        private void buttonImport_Click(object sender, EventArgs e)
        {
            if (dataGridView1.CurrentRow!=null)
            {
                String seqid = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["SEQID"].Value.ToString();

                String status = dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells["STATUS_INT"].Value.ToString();
                if (Convert.ToInt32(status) != Constant.RENT_CONTRACT_STATUS_SHENGXIAO) {
                    MessageBox.Show("此合同已经不在执行,不需终止.", "提示");
                    return;
                }

                int seqidInt = Convert.ToInt32(seqid);
                if (seqidInt > 0)
                {


                    DialogResult dialogResult = MessageBox.Show("请确认是否要将选择的终止合同?终止合同前请处理完押金与房租及其它费用.合同一量终止.所有操作都将冻结.", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);

                    if (dialogResult.Equals(DialogResult.OK))
                    {
                        bllRENT_CONTRACT.DongzhiContract(seqidInt);
                        //dal.Delete(seqidInt);
                        //刷新数据
                        refreshData(CURRENT_PAGE);
                    }


                }
            }
            else
            {
                MessageBox.Show("请选择要终止的记录.", "提示");
            }  
            
        }

        private void UserControlRentContract_Load(object sender, EventArgs e)
        {
            this.refreshData(this.CURRENT_PAGE);
        }
        //双击更新
        private void dataGridView1_CellDoubleClick_1(object sender, DataGridViewCellEventArgs e)
        {
            buttonUpdate_Click(null, null);
        }
        //双击更新设备
        private void dataGridView2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //合同界面不更新.
        }

        private void dataGridView2_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView2.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[0].Value = row.Index + 1;
            }
        }

       

       
    }
}
